import java.util.ArrayList;

public class StockRepository {

	private static StockRepository repository;
	private static ArrayList<Stock> stocks = new ArrayList<Stock>();

	private StockRepository() {
	}

	public static StockRepository getStockRepository() {
		if (repository == null) {
			repository = new StockRepository();
		}
		return repository;
	}

	public void addStock(Stock stock) {
		stocks.add(stock);
	}

	public ArrayList<Stock> getStocks() {
		return stocks;
	}

	public Stock getStock(int stockId) {
		for (int i = 0; i < stocks.size(); i++) {
			Stock stock = stocks.get(i);
			if (stock.getStockId() == stockId) {
				return stocks.get(i);
			}
		}
		return null;
	}

	public double getCurrentStockPrice(int stockId) {
		for (int i = 0; i < stocks.size(); i++) {
			Stock stock = stocks.get(i);
			if (stock.getStockId() == stockId) {
				return stocks.get(i).getStockPrice();
			}
		}
		return (Double) null;
	}

	public ArrayList<Stock> getAllStocks(int portfolioId) {
		ArrayList<Stock> allStocks = new ArrayList<Stock>();
		for (int i = 0; i < stocks.size(); i++) {
			Stock stock = stocks.get(i);
			if (stock.getPortfolioId() == portfolioId) {
				allStocks.add(stock);
			}
		}
		return allStocks;
	}

}
